import os
import time

from selenium.webdriver.support.wait import WebDriverWait

from auto_frame.base import Base
from page.contact_page import ContactPage
from data_file_analysis.yaml_analysis import YamlAnalysis as ya
from logs.logger import Logger

#通讯录添加成员
class MemberOperation(Base):

    def __init__(self, driver):
        super(MemberOperation, self).__init__(driver)
        cur_path = os.path.abspath(os.path.dirname(__file__))  # 获取当前文件目录
        self.root_path = os.path.dirname(cur_path)
        self.step_path = f'{self.root_path}/steps/member_operation.yaml'

    #输入必填项添加成员，暂时只考虑点击保存按钮情况
    def add_simple_member(self, data_dict:dict):
        self.steps(self.step_path, "add_simple_member", data_dict)
        self.driver.refresh()
        return ContactPage(self.driver)

    #输入非必填项添加成员
    def add_detail_member(self, data_dict:dict):
        if "性别" in data_dict.keys():
            if data_dict["性别"] == "男":
                data_dict["性别"] = 1
            elif data_dict["性别"] == "女":
                data_dict["性别"] = 2
            else:
                raise Exception("性别错误")

        if "身份" in data_dict.keys():
            if data_dict["身份"] == "普通成员":
                data_dict["身份"] = 0
            elif data_dict["身份"] == "上级":
                data_dict["身份"] = 1
            else:
                raise Exception("身份错误")

        self.steps(self.step_path, "add_detail_member", data_dict)

        #更新部门操作
        if "部门" in data_dict.keys():
            eles = ya.return_yaml_data(self.step_path, 'modify_department')
            self.log.logger.info("modify_department")

            self.log.logger.info(f"by_type:{eles[0]['by']}, locator:{eles[0]['locator']}")
            self.driver.find_element(eles[0]['by'], eles[0]['locator']).click()

            self.log.logger.info(f"by_type:{eles[1]['by']}, locator:{eles[1]['locator']}")
            ele_dialog = self.driver.find_element(eles[1]['by'], eles[1]['locator'])

            self.log.logger.info(f"by_type:{eles[2]['by']}, locator:{eles[2]['locator']}")
            element = ele_dialog.find_element(eles[2]['by'], eles[2]['locator'])
            time.sleep(3)
            element.send_keys(data_dict['部门'])
            time.sleep(3)

            self.log.logger.info(f"by_type:{eles[3]['by']}, locator:{eles[3]['locator']}")
            self.driver.find_element(eles[3]['by'], eles[3]['locator']).click()

            self.log.logger.info(f"by_type:{eles[4]['by']}, locator:{eles[4]['locator']}")
            self.driver.find_element(eles[4]['by'], eles[4]['locator']).click()

        self.steps(self.step_path, "save_member")
        self.driver.refresh()
        return ContactPage(self.driver)

    #编辑成员
    def edit_member(self):
        pass